﻿using System;
using System.IO;
using FFmpegVideoClip.Mode.Files;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;


namespace FFmpegVideoClip.Lib.AdvancedFragmentProcessor
{
    public class ChineseTSConverts
    {
        /// <summary>
        /// 转换方向类型的公约签名
        /// </summary>
        public const string TSConverts = "#*TSC*#";
        /// <summary> 
        /// 简体转换为繁体
        /// </summary> 
        /// <param name="str">简体字</param> 
        /// <returns>繁体字</returns> 
        public static string GetTraditional(string str)
        {
            string resultstr = string.Empty;
            resultstr = ChineseConverter.Convert(str, ChineseConversionDirection.SimplifiedToTraditional);
            return resultstr;
        }
        /// <summary> 
        /// 繁体转换为简体
        /// </summary> 
        /// <param name="str">繁体字</param> 
        /// <returns>简体字</returns> 
        public static string GetSimplified(string str)
        {
            string resultstr = string.Empty;
            resultstr = ChineseConverter.Convert(str, ChineseConversionDirection.TraditionalToSimplified);
            return resultstr;
        }

        public static void Convert(AdvancedParametersSchemeModule Module, SuccessAndProgress successAndProgress)
        {
            ParametersSign? Sign;
            string Result = string.Empty;
            if (Module.GetParametersSign(TSConverts, out Sign) && (Sign?.Parameter.Equals("S", StringComparison.OrdinalIgnoreCase) ?? false))
                Result = GetSimplified(File.ReadAllText(Module.GetInput().Parameter));
            else if (Module.GetParametersSign(TSConverts, out Sign) && (Sign?.Parameter.Equals("T", StringComparison.OrdinalIgnoreCase) ?? false))
                Result = GetTraditional(File.ReadAllText(Module.GetInput().Parameter));
            else throw new ArgumentException($"字幕转码参数错误：支持的参数[简体(S),繁体(T)] 当前值：{Sign?.Parameter}");

            File.WriteAllText(Module.GetOutput().Parameter, Result);

            successAndProgress.IsSuccess = true;
        }
        
    }
}
